﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using System.Windows.Forms;
namespace ForceSynch
{

    public class DebugTraceListener : TraceListener
    {
        private string _file;
      private  TextWriterTraceListener _log;
        private object _lock=new object();
        public DebugTraceListener(string filename)
        {
            this._file = filename;
            _log = new TextWriterTraceListener(_file + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".log");
        }
        public override void Write(object message)
        {
            lock (_lock)
            {
                Write(message.ToString());
            }
        }

        public override void Write(string message)
        {
            lock (_lock)
            {
                _log.WriteLine(DateTime.Now.ToShortDateString()+" " +DateTime.Now.ToShortTimeString()+":"+ message);
                _log.Flush();
            }
        }

        public override void WriteLine(string message)
        {
            lock (_lock)
            {
                Write(message+"\r\n");
            }
        }
         ~DebugTraceListener()
        {
            try
            {
                _log.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }

}
